/*广度优先搜索 */

class Solution {
    public int maxMoves(int[][] grid) {
        int m = grid.length, n = grid[0].length, res = 0;
        int[] prev_access = new int[m], access = new int[m];
        for (int i = 0; i < m; i++) {
            access[i] = 1;
        }
        for (int i = 1; i < n; i++) {
            prev_access = access;
            access = new int[m];
            for (int j = 0; j < m; j++) {
                for (int k = -1; k < 2; k++) {
                    if (j+k >=0 && j+k <m) {
                        if (prev_access[j+k] == 1 && grid[j+k][i-1]<grid[j][i]){
                            access[j] = 1;
                            res = i;
                            break;
                        }
                    }
                }
            }
            if (res < i) return res;
        }
        return res;
    }
}